<?php

//接值管理员信息
$admin = $_POST['admin'];
$pwd = $_POST['pwd'];
$repwd = $_POST['repwd'];

//判断密码是否一致
if($pwd != $repwd){
	exit('<script>alert("密码不一致");window.location.href="./install3.php";</script>');
}else{
	$newpwd = md5($pwd); 
}

//将数据库信息拼接为字符串
$str = "<?php\n";
foreach($_POST as $k => $v){
	//跳过管理员信息
	if($k == $admin){
		break;
	}
		$str .= "const {$k} = '{$v}';\n";
}

//覆盖写入到文件中
if(file_put_contents('../public/peizhi.php',$str)){
	//包含配置
	include '../public/peizhi.php';
		//连接数据库
		$link = @mysqli_connect(DB_HOST,DB_USER,DB_PASS);
		if(mysqli_connect_errno()){
			echo '连接数据库失败'.mysqli_connect_error();
		}
		//建库
		$dbsql = 'create database if not exists '.DB_NAME;
		if(mysqli_query($link,$dbsql)){
			//连接数据库
			if(!mysqli_select_db($link,DB_NAME)){
				echo '选择数据库失败';
			}
			mysqli_set_charset($link,DB_CHARSET);
			//将配置文件读出来,分割为数组
			$sqlstr = file_get_contents('./luntan.sql');
			$arr = explode(';',$sqlstr);
			array_pop($arr);

			foreach($arr as $key => $val){
				if(mysqli_query($link,$val)){
					echo '第'.($key+1).'个表创建成功<br><br>';
				}else{
					echo '第'.($key+1).'个表创建失败<br><br>';
				}	
			}

			//添加管理员
			$addsql = "insert into user(name,pass,qx) values('{$admin}','{$newpwd}','2')";
			if(!mysqli_query($link,$addsql)){
				exit('<script>alert("管理员账户添加失败");window.location.href="./install3.php";</script>');
			}
			
			//生成文件锁
			file_put_contents('install.txt','ok !!!');
			
			echo '<script>alert("安装成功");window.location.href="../index.php";</script>';
			
	}else{
			exit('<script>alert("创建库失败");window.location.href="./install3.php";</script>');
	}
}else{
	exit('<script>alert("配置文件修改失败");window.location.href="./install3.php";</script>');
}




